﻿#region Copyright © 2009 by Bj Rollison
/// <copyright> Copyright © 2009 by Bj Rollison
/// This source is subject to the Microsoft Public License (Ms-PL).
/// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
/// All other rights reserved.
/// </copyright>
#endregion

namespace TestingMentor.StatisticalSampleSizeCalculator
{
  using System;

  /// <summary>
  /// Calculator class provides the properties to set the required variables 
  /// and the method that performs the calculation
  /// </summary>
  internal class Calculator
  {
    #region PRIVATE FIELDS
    /// <summary>
    /// Variable for standard deviation used in sample size calculation.
    /// Default is 50% - maximum value.
    /// </summary>
    private static double standardDeviationValue = .5;
    
    /// <summary>
    /// Variable for population size used in sample size calcuation.
    /// Default value is 1000.
    /// </summary>
    private static int populationValue = 1000;
    
    /// <summary>
    /// Variable for confidence level used in sample size calculation.
    /// Default value is 95%.
    /// </summary>
    private static double confidenceLevelValue = 1.96;
    
    /// <summary>
    /// Variable for sampling error used in sample size calculation.
    /// Default value is 5%
    /// </summary>
    private static double samplingErrorValue = .05;
    #endregion PRIVATE FIELDS

    #region PRIVATE CONSTRUCTOR
    /// <summary>
    /// Prevents a default instance of the Calculator class from being created.
    /// </summary>
    private Calculator() { }
    #endregion PRIVATE CONSTRUCTOR

    #region PUBLIC PROPERTIES
    /// <summary>
    /// Sets the standard deviation variable
    /// </summary>
    public static double SetStandardDeviation
    {
      set { standardDeviationValue = value; }
    }

    /// <summary>
    /// Sets the population size variable
    /// </summary>
    public static int SetPopulationSize
    {
      set { populationValue = value; }
    }

    /// <summary>
    /// Sets the confidence level variable
    /// </summary>
    public static double SetConfidenceLevel
    {
      set { confidenceLevelValue = value; }
    }

    /// <summary>
    /// Sets the sampling error variable
    /// </summary>
    public static double SetSamplingErrorRate
    {
      set { samplingErrorValue = value; }
    }

    /// <summary>
    /// Gets the sample size value
    /// </summary>
    public static int GetSampleSize
    {
      get { return CalculateSampleSize(); }
    }
    #endregion PUBLIC PROPERTIES

    #region PRIVATE METHODS
    /// <summary>
    /// Calcuates statistical sample size for specified level of confidence
    /// </summary>
    /// <returns>Integer value representing the size of the sample </returns>
    private static int CalculateSampleSize()
    {
      double result = 0;
      result = (Math.Pow(confidenceLevelValue, 2) * Math.Pow(standardDeviationValue, 2) /
        Math.Pow(samplingErrorValue, 2)) / (1 + ((Math.Pow(confidenceLevelValue, 2) * 
        Math.Pow(standardDeviationValue, 2) / Math.Pow(samplingErrorValue, 2)) - 1) / populationValue);
      return (int)result;
    }
    #endregion PRIVATE METHODS
  }
}
